
/**
 * AppleIIGo
 * GCR 6&2 Disk Image class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "FileDiskImage.h"

#ifndef GCRDISKIMAGE_H
#define GCRDISKIMAGE_H

typedef enum {
	GCRTYPE_6_2,
	GCRTYPE_5_3,
};

class GCRDiskImage : public FloppyDiskImage {
public:
	GCRDiskImage(UINT32 diskType,
				 char * diskImagePath,
				 UINT32 gcrType);
	
protected:
	virtual void getDiskTrack(UINT32 * trackIndex, UINT32 * headIndex,
							  UINT32 * diskImageTrackByteNum,
							  UINT32 diskImageTrackIndex) = 0;
	virtual void getDiskImageTrack(UINT32 * diskImageTrackIndex,
								   UINT32 * diskImageTrackByteNum,
								   UINT32 trackIndex, UINT32 headIndex) = 0;
	
	void initGcrChecksum();
	
	void writeSync(UINT32 syncNum, UINT32 syncBitNum, UINT32 lastSyncBitNum);
	void writeFMValue(UINT8 value);
	void writeGcrValue(UINT8 value);
	void writeGcrChecksum();
	virtual void writeAddressField(UINT32 diskImageTrackIndex,
								   UINT32 sectorIndex) = 0;
	virtual void writeDataField(UINT8 * sectorData) = 0;
	virtual void writeTrack(UINT32 diskImageTrackIndex,
							UINT8 * diskImageTrackData) = 0;
	bool readDiskImageTrack(UINT32 diskImageTrackIndex);
	bool readDiskImage(UINT32 diskImageOffset,
					   UINT32 diskImageTrackNum);
	
	UINT8 readFMValue();
	UINT8 readGcrValue();
	bool isGcrError();
	bool checkGcrChecksum();
	virtual bool readAddressField(UINT32 diskImageTrackIndex,
								  UINT32 sectorIndex) = 0;
	virtual bool readDataField(UINT8 * sectorData) = 0;
	virtual bool readTrack(UINT32 diskImageTrackIndex,
						   UINT8 * diskImageTrackData) = 0;
	bool writeDiskImageTrack(UINT32 trackIndex, UINT32 headIndex, bool isWrite);
	bool writeDiskImage(UINT32 diskImageOffset, bool isWrite);
	
private:
	UINT8 * gcrWriteMap;
	UINT8 gcrReadMap[0x100];
	
	UINT8 gcrChecksum;
	bool isGcrErrorValue;
};

#endif
